package com.company.cuobuliao.ui.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.ab.util.AbToastUtil;
import com.company.cuobuliao.R;
import com.company.cuobuliao.base.BaseActivity;
import com.company.cuobuliao.ui.home.calendar.DateAdapter;
import com.company.cuobuliao.ui.home.calendar.DateUtils;
import com.company.cuobuliao.view.ThinFontTextView;

/**
 * Created by Oreo on 2017/8/16.
 */

public class ChooseTimeActivity extends BaseActivity implements View.OnClickListener {

    private GridView gv;//定义gridView
    private DateAdapter dateAdapter;//定义adapter
    private int year, Year;
    private int month, Month;
    private int day, Day;
    private String title;
    private int[][] days = new int[6][7];

    private int[] daysList = new int[42];

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choosetime);
        //初始化日期数据
        initData();
        //初始化组件
        initView();
    }

    private void initData() {
        back(R.id.rllyBack);
        
        Year = DateUtils.getYear();
        Month = DateUtils.getMonth();
        Day = DateUtils.getDay();
        year = DateUtils.getYear();
        month = DateUtils.getMonth();
        day = DateUtils.getDay();
        setTile();
    }

    private void initView() {
        findViewById(R.id.ivLeft).setOnClickListener(this);
        findViewById(R.id.ivRight).setOnClickListener(this);

        gv = (GridView) findViewById(R.id.record_gridView);
        days = DateUtils.getDayOfMonthFormat(Year, Month);

        int dayNum = 0;
        //将二维数组转化为一维数组，方便使用
        for (int i = 0; i < days.length; i++) {
            for (int j = 0; j < days[i].length; j++) {
                daysList[dayNum] = days[i][j];
                dayNum++;
            }
        }

        dateAdapter = new DateAdapter(this, days, day);//传入当前月的年
        gv.setAdapter(dateAdapter);
        gv.setVerticalSpacing(40);

        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (i < 7 && daysList[i] > 20) {
                    if (month == 1) {
                        AbToastUtil.showToast(ChooseTimeActivity.this, (year - 1) + "年" + "12月" + daysList[i] + "日");
                    } else {
                        AbToastUtil.showToast(ChooseTimeActivity.this, year + "年" + (month - 1) + "月" + daysList[i] + "日");
                    }
                } else if (i > 20 && daysList[i] < 15) {
                    if (month == 12) {
                        AbToastUtil.showToast(ChooseTimeActivity.this, (year + 1) + "年" + "1月" + daysList[i] + "日");
                    } else {
                        AbToastUtil.showToast(ChooseTimeActivity.this, year + "年" + (month + 1) + "月" + daysList[i] + "日");
                    }
                } else {
                    AbToastUtil.showToast(ChooseTimeActivity.this, year + "年" + month + "月" + daysList[i] + "日");
                }
            }
        });
    }

    /**
     * 下一个月
     */
    private int[][] nextMonth() {
        if (month == 12) {
            month = 1;
            year++;
        } else {
            month++;
        }
        days = DateUtils.getDayOfMonthFormat(year, month);
        return days;
    }

    /**
     * 上一个月
     */
    private int[][] prevMonth() {
        if (month == 1) {
            month = 12;
            year--;
        } else {
            month--;
        }
        days = DateUtils.getDayOfMonthFormat(year, month);
        return days;
    }

    /**
     * 设置标题
     */
    private void setTile() {
        title = year + "年" + month + "月";
        ((ThinFontTextView) findViewById(R.id.tvTime)).setText(title);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ivLeft:
                days = prevMonth();
                if (Year == year && Month == month) {
                    dateAdapter = new DateAdapter(this, days, day);
                } else {
                    dateAdapter = new DateAdapter(this, days, 0);
                }
                gv.setAdapter(dateAdapter);
                dateAdapter.notifyDataSetChanged();
                setTile();
                break;
            case R.id.ivRight:
                days = nextMonth();
                if (Year == year && Month == month) {
                    dateAdapter = new DateAdapter(this, days, day);
                } else {
                    dateAdapter = new DateAdapter(this, days, 0);
                }
                gv.setAdapter(dateAdapter);
                dateAdapter.notifyDataSetChanged();
                setTile();
                break;
        }
    }
}
